package com.cn.springcloudshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.springcloudshop.common.AjaxRes;
import com.cn.springcloudshop.common.Const;
import com.cn.springcloudshop.model.*;
import com.cn.springcloudshop.service.inter.ProductService;
import com.cn.springcloudshop.service.inter.ShopCarService;
import com.cn.springcloudshop.service.inter.WeChatFeignClient;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: wuyanzu
 * @Date: 2018/7/31
 */
@RequestMapping("/shopCar/")
@RestController
public class ShopCarController {

    @Autowired
    ShopCarService shopCarService;

    @Autowired
    WeChatFeignClient weChatFeignClient;

    @Autowired
    ProductService productService;

    @RequestMapping("hi")
    public String test() {
        System.err.println(weChatFeignClient.getTestUser().getRegisterChannelId());
        return weChatFeignClient.hi();
    }

    /**
    *
    * 根据主键删除购物车信息
    * @author wuyanzu
    * @date 2018/7/31 11:00
    * @param [shopCarId]
    * @return int
    */
    @RequestMapping("del")
    public AjaxRes deleteByPrimaryKey(Long shopCarId,AjaxRes ajaxRes) {
        if (shopCarId != null) {
            if (shopCarService.deleteByPrimaryKey(shopCarId) == 1) {
                ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
            } else {
                ajaxRes.setFailMsg(Const.DEL_FAIL);
            }
        } else {
            ajaxRes.setFailMsg(Const.PARAMETER_NULL);
        }
        return ajaxRes;
    }

    /**
    *
    * 添加购物车
    * @author wuyanzu
    * @date 2018/7/31 17:26
    * @param [productId, number, openid]
    * @return int
    */
    @RequestMapping("add")
    public AjaxRes insShopCar(AjaxRes ajaxRes, String ymUser,Long productId, Long number) {
        YmUser user = getYmUser(ymUser);
        System.err.println(user.getUserId()+"==============");
        if (user != null) {
            YmShopCar ymShopCar = new YmShopCar();
            ymShopCar.setShopCarProductNumber(number);
            ymShopCar.setShopCarProductId(productId);
            ymShopCar.setShopCarOpenid(user.getRegisterChannelId());
            if (shopCarService.insert(ymShopCar) == 1) {
                ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
            } else {
                ajaxRes.setFailMsg(Const.SAVE_FAIL);
            }
        } else {
            ajaxRes.setFailMsg("用户不能为空");
        }

        return ajaxRes;
    }

    public static YmUser getYmUser(String ymUser) {
        if (StringUtils.isNotEmpty(ymUser)) {
            YmUser user = JSONObject.parseObject(ymUser, YmUser.class);
            if (user != null) {
                return user;
            }
        }
        return null;
    }

    /**
    *
    * 获取用户所有购物车信息
    * @author wuyanzu
    * @date 2018/7/31 17:26
    * @param [openid]
    * @return java.util.List<com.cn.springcloudshop.model.YmShopCar>
    */
    @RequestMapping("getAll")
    public AjaxRes getAllShopCar(AjaxRes ajaxRes,String ymUser) {
        YmUser user = getYmUser(ymUser);
        System.err.println(user.getUserId()+"==============");
        if (user != null) {
            YmShopCarExample ymShopCarExample = new YmShopCarExample();
            ymShopCarExample.setDistinct(false);
            ymShopCarExample.createCriteria().andShopCarOpenidEqualTo(user.getRegisterChannelId());
            List<YmShopCar> list = shopCarService.selectByExample(ymShopCarExample);
            if (list != null && list.size() > 0) {
                List<ShopCarVo> shopCarVos = new ArrayList<>();
                ShopCarVo shopCarVo = null;
                for (YmShopCar ymShopCar : list) {
                    shopCarVo = new ShopCarVo();
                    shopCarVo.setShopCarId(ymShopCar.getShopCarId());
                    shopCarVo.setShopCarProductId(ymShopCar.getShopCarProductId());
                    shopCarVo.setShopCarProductNumber(ymShopCar.getShopCarProductNumber());
                    shopCarVo.setShopCarOpenid(ymShopCar.getShopCarOpenid());
                    YmShopProduct product = productService.selectByPrimaryKey(ymShopCar.getShopCarProductId());
                    shopCarVo.setProductName(product.getProductName());
                    shopCarVo.setProductPrice(product.getProductPrice());
                    shopCarVo.setProductPic(product.getProductPic());
                    shopCarVo.setProductPoint(product.getProductPoint());
                    shopCarVos.add(shopCarVo);
                }
                ajaxRes.setSucceed(shopCarVos, Const.DATA_SUCCEED);
            } else {
                ajaxRes.setFailMsg(Const.DATA_FAIL);
            }
        } else {
            ajaxRes.setFailMsg("用户不能为空");
        }

        return ajaxRes;
    }

    /**
    *
    * 批量删除购物车信息
    * @author wuyanzu
    * @date 2018/8/1 9:55
    * @param [shopCarIds]
    * @return int
    */
    @RequestMapping("delAll")
    public AjaxRes delAllShopCar(AjaxRes ajaxRes,List<Long> shopCarIds) {
        int res = shopCarService.deleteBatchByPK(shopCarIds);
        if (res > 0) {
            ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
        } else {
            ajaxRes.setFailMsg(Const.DEL_FAIL);
        }
        return ajaxRes;
    }

    /**
    *
    * 更新购物车信息
    * @author wuyanzu
    * @date 2018/8/21 15:14
    * @param [ajaxRes, shopCarId, number]
    * @return com.cn.springcloudshop.common.AjaxRes
    */
    @RequestMapping("update")
    public AjaxRes updateShopCar(AjaxRes ajaxRes, Long shopCarId, Long number) {
        if (shopCarId != null && number != null) {
            YmShopCar ymShopCar = shopCarService.selectByPrimaryKey(shopCarId);
            if (ymShopCar != null) {
                ymShopCar.setShopCarProductNumber(number);
                if (shopCarService.updateByPrimaryKey(ymShopCar) == 1) {
                    ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
                } else {
                    ajaxRes.setFailMsg(Const.UPDATE_FAIL);
                }
            } else {
                ajaxRes.setFailMsg(Const.DATA_FAIL);
            }
        } else {
            ajaxRes.setFailMsg(Const.PARAMETER_NULL);
        }

        return ajaxRes;
    }
}
